﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;


using Blogger.UI.Data;
using Blogger.UI.Models;

namespace Blogger.UI.Models.Controllers
{
    public class PostingController : ApiController
    {
        private IPostingRepository _postingRepository;             

        public  PostingController(IPostingRepository postingRepository)
        {
            _postingRepository = postingRepository;
        }
        // GET api/Posting
        public IEnumerable<Posting> GetPostings()
        {
            return _postingRepository.GetPostings();
        }

        // GET api/Posting/5
        public Posting GetPosting(string id)
        {
            Posting posting = _postingRepository.FindPosting(id);
            if (posting == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return posting;
        }

        // PUT api/Posting/5
        public HttpResponseMessage PutPosting(string id, Posting posting)
        {
            if (ModelState.IsValid && id == posting.ID)
            {
                

                try
                {
                    _postingRepository.UpdatePosting(posting);
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Posting
        public HttpResponseMessage PostPosting(Posting posting)
        {
            if (ModelState.IsValid)
            {
                _postingRepository.CreatePosting(posting);

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, posting);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = posting.ID }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Posting/5
        public HttpResponseMessage DeletePosting(string id)
        {
            Posting posting = _postingRepository.FindPosting(id);
            if (posting == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

          

            try
            {
                _postingRepository.DeletePosting(posting);
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, posting);
        }

        protected override void Dispose(bool disposing)
        {

            base.Dispose(disposing);
        }
    }
}